<template>
  <el-dialog
    title="基础信息修改"
    :visible.sync="visible"
    :append-to-body="true"
    :closeOnClickModal="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="头像" prop="avatar">
        <single-upload v-model="dataForm.avatar"></single-upload>
      </el-form-item>
      <!-- <el-form-item label="简介" prop="intro">
        <el-input type="" v-model="dataForm.intro"></el-input>
      </el-form-item>-->
      <el-form-item label="邮箱" prop="email">
        <el-input type v-model="dataForm.email"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { clearLoginInfo } from '@/utils';
import singleUpload from './common/upload/singleUpload.vue';
export default {
  components: { singleUpload },
  data() {
    return {
      visible: false,
      dataForm: {
        id: this.$store.state.user.id,
        avatar: this.$store.state.user.avatar,
        intro: '',
        email: '',
      },
      dataRule: {
        email: [
          {
            required: false,
            message: '邮箱地址不能为空,找回密码需要',
            trigger: 'blur',
          },
          {
            type: 'email',
            message: '请输入正确的邮箱地址,找回密码需要',
            trigger: ['blur', 'change'],
          },
        ],
      },
    };
  },
  computed: {
    userName: {
      get() {
        return this.$store.state.user.name;
      },
    },
  },
  methods: {
    // 初始化
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
      });
    },
    // 获取当前管理员信息
    getUserInfo() {
      var uid = this.$cookie.get('userId');
      this.$http({
        url: this.$http.adornUrl(`/edu-teacher/teacher/${uid}`), //http://localhost:8082/eduservice/edu-teacher/teacher/20205532
        method: 'get',
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        if (data && data.code === 20000) {
          this.loading = false;
          // userId.set(data.data.data.id);
          this.$store.commit('user/updateId', data.data.data.username);
          this.$store.commit('user/updateName', data.data.data.name);
          this.$store.commit('user/updateAvatar', data.data.data.avatar);
          this.$store.commit('user/updateUserType', this.$cookie.get('userType'));
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          var userType = this.$cookie.get('userType');
          var URLPATH = '';
          if (userType == 'student') {
            URLPATH = '/edu-student/updateInfo';
          }
          if (userType == 'teacher') {
            URLPATH = '/edu-teacher/updateInfo';
          }
          this.$http({
            url: this.$http.adornUrl(`${URLPATH}`),
            method: 'post',
            data: this.$http.adornData(
              {
                username: this.$store.state.user.id,
                avatar: this.dataForm.avatar,
                // intro: this.dataForm.intro,
                email: this.dataForm.email,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 20000) {
              this.$message({
                message: '修改信息成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                },
              });
              this.getUserInfo();
            } else {
              this.$message.error(data);
            }
          });
        }
      });
    },
  },
};
</script>

